package com.bms.common.base.config;

import java.util.Properties;

import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;

@Configuration
public class TransactionConfig {
	
	@Autowired
	private DataSourceTransactionManager transactionManager;

	@Bean(name = "txAdvice")
	public TransactionInterceptor getAdvisor() throws Exception {
		Properties properties = new Properties();
		properties.setProperty("get*", "PROPAGATION_REQUIRED,-Exception,readOnly");
		properties.setProperty("select*", "PROPAGATION_REQUIRED,-Exception,readOnly");
		properties.setProperty("query*", "PROPAGATION_REQUIRED,-Exception,readOnly");
		properties.setProperty("add*", "PROPAGATION_REQUIRED,-Exception");
		properties.setProperty("save*", "PROPAGATION_REQUIRED,-Exception");
		properties.setProperty("update*", "PROPAGATION_REQUIRED,-Exception");
		properties.setProperty("delete*", "PROPAGATION_REQUIRED,-Exception");
		return new TransactionInterceptor(transactionManager, properties);

	}

	@Bean
	public BeanNameAutoProxyCreator txProxy() {
		BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
		creator.setInterceptorNames("txAdvice");
		creator.setBeanNames("*Service", "*ServiceImpl");
		creator.setProxyTargetClass(true);
		return creator;
	}
}
